package com.ushine.web.communication.websocket;

import com.ushine.web.component.http.HttpResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: yls
 * @Date: 2021/1/21 14:46
 * @Description: 发送消息service层
 * @Version 1.0
 */
@Service
public class WebSocketService {
    @Autowired
    private SimpMessagingTemplate template;

    /**
     * 广播
     * 发给所有在线用户
     * @param msg 消息
     */
    public void sendMsg(HttpResponseVO msg) {
        template.convertAndSend(WsContant.PRODUCER_PATH, msg);
    }

    /**
     * 发送给指定用户
     * @param users 指定用户
     * @param msg 消息
     */
    public void send2Users(List<String> users, HttpResponseVO msg) {
        users.forEach(userName -> {
            template.convertAndSendToUser(userName, WsContant.P2P_PUSH_PATH, msg);
        });
    }
}
